package com.goods.business.service.imp;

import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private ProductMapper productMapper;


    @Override
    public List<ProductCategoryTreeNodeVO> getCategoryTree() {

        List<ProductCategory> list = productCategoryMapper.selectAll();
        ArrayList<ProductCategoryTreeNodeVO> nodeList = new ArrayList<>();
        for (ProductCategory productCategory : list) {
            ProductCategoryTreeNodeVO treeNodeVO = setVo(productCategory);
            nodeList.add(treeNodeVO);
        }
        return CategoryTreeBuilder.build(nodeList);
    }



    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> list = productCategoryMapper.selectAll();
        ArrayList<ProductCategoryTreeNodeVO> nodeList = new ArrayList<>();
        for (ProductCategory productCategory : list) {
            ProductCategoryTreeNodeVO treeNodeVO = setVo(productCategory);
            nodeList.add(treeNodeVO);
        }
        return CategoryTreeBuilder.buildParent(nodeList);
    }

    @Override
    public Integer addProductCategory(ProductCategory productCategory) {
        Date date = new Date();
        productCategory.setCreateTime(date);
        productCategory.setModifiedTime(date);
        return productCategoryMapper.insert(productCategory);


    }

    @Override
    public ProductCategory edit(Long id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer update(Long id, ProductCategory productCategory) {
        productCategory.setId(id);
        Date date = new Date();
        productCategory.setModifiedTime(date);
        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    @Override
    public Integer delete(Long id) {
        Integer result = 0;
        ProductCategory productCategory = new ProductCategory();
        productCategory.setPid(id);
        List<ProductCategory> select = productCategoryMapper.select(productCategory);
        if (!CollectionUtils.isEmpty(select)){
            result = 1;
        }

        //物资引用
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("oneCategoryId",id);
        criteria.orEqualTo("twoCategoryId",id);
        criteria.orEqualTo("threeCategoryId",id);
        Product product = productMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(product)){
            result = 2;
        }
        return result;
    }


    private ProductCategoryTreeNodeVO setVo(ProductCategory productCategory){
        ProductCategoryTreeNodeVO treeNode = new ProductCategoryTreeNodeVO();
        treeNode.setId(productCategory.getId());
        treeNode.setName(productCategory.getName());
        treeNode.setPid(productCategory.getPid());
        treeNode.setSort(productCategory.getSort());
        treeNode.setRemark(productCategory.getRemark());
        treeNode.setCreateTime(productCategory.getCreateTime());
        treeNode.setModifiedTime(productCategory.getModifiedTime());
        return treeNode;
    }








    /*   @Override
       public List<ProductCategoryTreeNodeVO> getCategoryTree() {
           ProductCategory category = new ProductCategory();
           category.setSort(3);
           List<ProductCategory> list3 = productCategoryMapper.select(category);
           category.setSort(2);
           List<ProductCategory> list2 = productCategoryMapper.select(category);
           category.setSort(1);
           List<ProductCategory> list1 = productCategoryMapper.select(category);


           List<ProductCategoryTreeNodeVO> treeNodeVOList2= new ArrayList<>();
           for (ProductCategory productCategory2 : list2) {
               ProductCategoryTreeNodeVO treeNode2 = setVo(productCategory2);
               List<ProductCategoryTreeNodeVO> treeNodeVOList3 = new ArrayList<>();
               for (ProductCategory productCategory3 : list3) {
                   if (productCategory3.getPid().equals(productCategory2.getId())) {
                       ProductCategoryTreeNodeVO treeNodeVO3 = setVo(productCategory3);
                       treeNodeVOList3.add(treeNodeVO3);
                   }
               }
               treeNode2.setChildren(treeNodeVOList3);
               treeNodeVOList2.add(treeNode2);
           }

           List<ProductCategoryTreeNodeVO> treeNodeVOList= new ArrayList<>();
           for (ProductCategory productCategory1 : list1) {
               ProductCategoryTreeNodeVO treeNode1 = setVo(productCategory1);
               //List<ProductCategoryTreeNodeVO> treeNodeVOList2 = new ArrayList<>();
               for (ProductCategory productCategory2 : list2) {
                   if (productCategory2.getPid().equals(productCategory1.getId())) {
                       ProductCategoryTreeNodeVO treeNodeVO2 = setVo(productCategory2);
                       treeNodeVOList2.add(treeNodeVO2);
                   }
               }
               treeNode1.setChildren(treeNodeVOList2);
               treeNodeVOList.add(treeNode1);
           }

               return treeNodeVOList;
       }
   */

}
